

function addReserveAccount() {
    let account = document.getElementById("reserve-account").value.trim();
    if (!account) {
        document.getElementById("reserve-result").innerText = "请输入保留账号";
        return;
    }
    fetch("/api/account/reserve", {
        method: "POST",
        headers: { "Content-Type": "application/json" },
        body: JSON.stringify({ account })
    })
        .then(res => res.json())
        .then(data => {
            document.getElementById("reserve-result").innerText = data.message;
            getReservedAccounts();
        })
        .catch(err => console.error(err));
}

function removeReserveAccount(account) {
    if (!account) {
        console.error("account 不能为空");
        return;
    }
    fetch("/api/account/unreserve", {
        method: "POST",
        headers: { "Content-Type": "application/json" },
        body: JSON.stringify({
            account: account
        })
    })
        .then(res => res.json())
        .then(data => {
            document.getElementById("reserve-result").innerText = data.message;
            getReservedAccounts();
        })
        .catch(err => console.error(err));
}

function getReservedAccounts() {
    fetch("/api/account/reserved")
        .then(res => res.json())
        .then(data => {
            data = JSON.parse(data.message)
            let list = document.getElementById("reserved-accounts-list");
            list.innerHTML = "";
            console.log(data)
            if (data.length > 0) {
                data.forEach(user => {
                    let li = document.createElement("li");
                    li.className = "list-group-item d-flex justify-content-between align-items-center";
                    li.innerText = user.account;
                    let btn = document.createElement("button");
                    btn.className = "btn btn-sm btn-danger";
                    btn.innerText = "删除";
                    btn.onclick = () => { removeReserveAccount(user.account); };
                    li.appendChild(btn);
                    list.appendChild(li);
                });
            } else {
                list.innerHTML = "<li class='list-group-item'>暂无保留账号</li>";
            }
            document.getElementById('all-reserved-count').textContent = data.length;
        })
        .catch(err => console.error(err));
}